React Js / APIs / post data using fetch()
Post Data
-
1. Step
1. define function
const handleCreate = async () => { const res = await fetch('https://jsonplaceholder.typicode.com/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'New User', email: 'new@example.com' }) }); const data = await res.json(); console.log('Created:', data); }; 1. method
2. headers
3. body
2. example
function App() { const handleCreate = async () => { const res = await fetch('https://jsonplaceholder.typicode.com/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'New User', email: 'new@example.com' }) }); const data = await res.json(); console.log('Created:', data); }; return ( ); } export default App;
MANVIA BLOG